var common = require("common");
cc.Class({
    extends: cc.Component,

    properties: {
        zhujiemianBtn:cc.Button,
        guanqiashu:cc.Label,
        gameview:cc.Node,
        touxiang:cc.Sprite,
        xiangcha:cc.Label,
        wenhaoBtn:cc.Button,
        tishiBtn:cc.Button,
        chongwanBtn:cc.Button,
        tianzi:cc.Node,
        zenmewan:cc.Node,
        guanbizw:cc.Button,
        restart:cc.Node,
        guanbirs:cc.Button,
        quedingBtn:cc.Button,
        levels:[],
        arr:[],
        top:[],
        Word:[],
        Idiom:[],
        array:[],
        topsprite:cc.Prefab,
        downsprite:cc.Prefab,
        over:cc.Node,
    },
    onLoad () 
    {
        this.zhujiemianBtn.node.on("click",this.OnZhuBtn,this);
        this.wenhaoBtn.node.on("click",this.OnWenHaoBtn,this);
        this.tishiBtn.node.on("click",this.OnTiShiBtn,this);
        this.chongwanBtn.node.on("click",this.OnChongWanBtn,this);
        this.zenmewan = cc.find("Canvas/Game/zenmewan");
        this.guanbizw.node.on("click",this.OnGuanBiZWBtn,this);
        this.restart = cc.find("Canvas/Game/restart");
        this.guanbirs.node.on("click",this.OnGuanBiRSBtn,this);
        this.quedingBtn.node.on("click",this.OnQDBtn,this);

        for (let i = 0; i < 81; i++) {
            let fangkuai = cc.instantiate(this.topsprite);
            fangkuai.parent = this.gameview;
            fangkuai.name = "topsprite" + i;
        }
        for (let i = 0; i < 36; i++) {
            let fangkuai = cc.instantiate(this.downsprite);
            fangkuai.parent = this.tianzi;
            fangkuai.name = "downsprite" + i;
        }
         //读取配置表
        var self = this;
        cc.loader.loadRes("peizhi/levels",function(error,TestJson) {
            self.levels = TestJson.json;
            self.node.emit("LoadFinish",self.levels,common.level);
        });
        this.node.on("LoadFinish",function() {
            this.GoToLevel();
            this.GoIdiom();
        },this);

        //生成随机数
        for (let i = 0; i < 36; i++) {
            this.array[i] = Math.floor(Math.random()*36);
            for (let j = 0; j < i; j++) {
                if (this.array[i] == this.array[j]) {
                    i--;
                    break;
                }
            }
        }
    },
    OnZhuBtn:function(event) 
    {
        this.node.active = false;
        // cc.director.loadScene("Hall");
    },
    OnWenHaoBtn:function(event) 
    {
        this.zenmewan.active = true;
    },
    OnGuanBiZWBtn:function(event) 
    {
        this.zenmewan.active = false;
    },
    OnTiShiBtn:function(event) 
    {
        
    },
    OnChongWanBtn:function(event) 
    {
        this.restart.active = true;
    },
    OnGuanBiRSBtn:function(event) 
    {
        this.restart.active = false;
    },
    OnQDBtn:function(event) 
    {
        //重新开始游戏

    },
    //读取关卡数
    GoToLevel() {
        console.log("LoadFinish");
        this.gameview.getComponent(cc.Layout).enabled = false;
        let lines = this.levels[common.level].lines;
        this.top = this.gameview.getChildren();    //字块
        for (let i = 0; i < this.top.length; i++) {
            this.top[i].active = true;
        }
        let down = this.tianzi.getChildren();     //选择填字
        for (let i = 0; i < down.length; i++) {
            down[i].active = true;
        }
        var vIndex = 0;
        var hanzi = 0;
        var self = this;
        for (let i = 0; i < lines.length; i++) {
            let label = this.top[i].getChildByName("Label").getComponent(cc.Label);
            this.top[i].on("mousedown",function(event) {
                if (event.target.name != "finish") {
                    common.selectNode = event.target;
                    common.selectNode.getChildByName("tupian").active = true;
                }
                let shzi = event.target.getChildByName("Label").getComponent(cc.Label);
                let yanse = event.target.getChildByName("Background");
                if (shzi.string != "") {
                    for (let i = 0; i < down.length; i++) {
                        let dizi = down[i].getChildByName("Label").getComponent(cc.Label);
                        if (shzi.string == dizi.string) {
                            down[i].active = true;
                        }
                    }
                    shzi.string = "";
                    yanse.color = cc.color(255,255,255,255);
                }
            });
            if (lines[i].c != "？") {
                if (lines[i].v == 1) {
                    label.string = lines[i].c;
                }else if(lines[i].v == 0){
                    label.string = "";
                    this.arr.push(i);
                    let wenben = down[this.array[vIndex]].getChildByName("Label").getComponent(cc.Label);
                    wenben.string = lines[i].c;
                    //添加点击事件
                    down[this.array[vIndex]].on("mousedown",function(event) {
                        var node = event.target;
                        let zi = common.selectNode.getChildByName("Label").getComponent(cc.Label);
                        let downLabel = node.getChildByName("Label").getComponent(cc.Label);
                        if (zi.string == "") {
                            node.active = false;
                            zi.string = downLabel.string;  
                        }else{
                            let string = zi.string;
                            zi.string = downLabel.string;
                            downLabel.string = string;
                        }
                        let name = common.selectNode.name;
                        let hao = name.replace("topsprite","");
                        if (zi.string == lines[hao].c) {
                            self.top[hao].getChildByName("finish").active = true;
                            self.top[hao].getChildByName("Label").getComponent(cc.Label).string = zi.string;
                            hanzi++;
                            self.NextSelect();   //找下一个方块
                            if (hanzi == vIndex) {
                                self.over.active = true;
                                // cc.director.loadScene("Over");
                            }
                        }else{
                            //填入错字
                            self.top[hao].getChildByName("Background").color = cc.color(182,0,0,255);
                        }
                    });
                    vIndex++;
                }
            }else{
                this.top[i].active = false;   //隐藏顶部多余方块
            }
        }
        
        //隐藏底部多余方块
        for (let i = 0; i < down.length; i++) {
            if (down[i].getChildByName("Label").getComponent(cc.Label).string == "") {
                down[i].active = false;
            }
        }  
        // var num = 0;
        // for (let i = 0; i < down.length; i++) {
        //     if (down[i].getChildByName("Label").getComponent(cc.Label).string != "") {
        //         num++;
        //         if (num == vIndex) {
        //             console.log(vIndex + "vIndex------------------");
        //             this.tianzi.getComponent(cc.Layout).enabled = false;
        //         }
        //     }
        // }

        common.selectNode = this.top[this.arr[0]];    //上方填入方块
    },
    //start () {},

    //读取成语
    GoIdiom(){
        let lines = this.levels[common.level].lines;
        for (let i = 0; i < 81; i++) {
            if (lines[i].c != "？") {
                if (i%9 < 6) {
                    if (lines[i + 1].c != "？" && lines[i + 2].c != "？" && lines[i + 3].c != "？") {
                        this.Word.push(lines[i].c);
                        this.Word.push(lines[i+1].c);
                        this.Word.push(lines[i+2].c);
                        this.Word.push(lines[i+3].c);
                    }
                }
                if (i < 54) {
                    if(lines[i + 9].c != "？" && lines[i + 2*9].c != "？" && lines[i + 3*9].c != "？"){
                        this.Word.push(lines[i].c);
                        this.Word.push(lines[i+9].c);
                        this.Word.push(lines[i+2*9].c);
                        this.Word.push(lines[i+3*9].c);
                    }
                }
            }
        }
        let zifuchuan = this.Word.join("");
        for (let i = 0; i < zifuchuan.length;) {
            let cy = zifuchuan.substr(i,4);
            i = i + 4;
            this.Idiom.push(cy);
        }
        common.Idiom = this.Idiom;
    },
    NextSelect(){
        for (let i = 0; i < this.arr.length - 1; i++) {
            let zi = this.top[this.arr[i]].getChildByName("Label").getComponent(cc.Label).string;
            if (zi != "") {
                common.selectNode = this.top[this.arr[i+1]];
            }
        }
    },
    update (dt) 
    {
        for (let i = 0; i < this.top.length; i++) {
            this.top[i].getChildByName("tupian").active = false;
            if (common.selectNode == this.top[i]) {
                common.selectNode.getChildByName("tupian").active = true;
            }
        }
        if (common.tilitubiao.active) {
            
        }
    },
});